package cn.edu.live.interceptor;

import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

public class CheckIfLoginInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation ai) {
		int code = -1;
		String msg = "您未登录！请先登录！";

		HttpSession session = ai.getController().getSession();
		if (session == null) {// 貌似session不会为空？
			ai.getController().setAttr("respcontent", msg);
			ai.getController().renderJsp("/login.jsp");
			System.out.println("Checked session is null.");
			return;
		} else {
			String username = (String) session.getAttribute("username");
			if (username != null) {
				// 已登录则啥都不干，否则就重定向到登陆界面
				ai.invoke();
				System.out.println("Checked success.user is " + username);
			} else {
				// 这里要提示没登陆的信息，否则可以直接
				// ai.getController().redirect("/user/loginPage");
				ai.getController().setAttr("respcontent", msg);
				ai.getController().renderJsp("/login.jsp");
				System.out.println("Checked username is null.");
				return;
			}
		}// end else
	}

}
